'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _objectWithoutProperties2 = _interopRequireDefault(
  require('@babel/runtime/helpers/objectWithoutProperties'),
);
var _react = _interopRequireWildcard(require('react'));
var _antd = require('antd');
var _createField = _interopRequireDefault(require('../../../createField'));
var _useOptions2 = _interopRequireDefault(require('../../../hooks/useOptions'));
var _useSingleOther = _interopRequireDefault(require('../../../hooks/useSingleOther'));
var _FieldContext = require('../../../contexts/FieldContext');
var _excluded = ['value', 'onChange', 'options'];
/**
 * 性能未必可靠，选择其他失焦校验不好
 * @param param0
 * @returns
 */
var SelectWithOther = function SelectWithOther(_ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    optionsProp = _ref.options,
    rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useOptions = (0, _useOptions2.default)(optionsProp),
    options = _useOptions.options;
  var _useFieldContext = (0, _FieldContext.useFieldContext)(),
    label = _useFieldContext.label;
  var otherRef = (0, _react.useRef)('select-other-' + Math.random());
  var _useOther = (0, _useSingleOther.default)({
      value: value,
      onChange: onChange,
      options: options,
    }),
    otherChecked = _useOther.otherChecked,
    changeOtherChecked = _useOther.changeOtherChecked,
    selectedValue = _useOther.selectedValue,
    changeSelectedValue = _useOther.changeSelectedValue,
    otherValue = _useOther.otherValue,
    changeOtherValue = _useOther.changeOtherValue;
  var handleSelectChange = (0, _react.useCallback)(
    function (val) {
      var fnd = options.find(function (op) {
        return op.value === val;
      });
      var newVal;
      if (fnd) {
        newVal = fnd.value;
        changeSelectedValue(newVal);
      } else {
        newVal = undefined;
        changeOtherChecked(true);
      }
    },
    [options, onChange],
  );
  var handleInputChange = (0, _react.useCallback)(function (event) {
    changeOtherValue(event.target.value);
  }, []);
  return /*#__PURE__*/ _react.default.createElement(
    _react.Fragment,
    null,
    /*#__PURE__*/ _react.default.createElement(
      _antd.Select,
      (0, _objectSpread2.default)(
        (0, _objectSpread2.default)(
          {
            value: otherChecked ? otherRef.current : selectedValue,
            onChange: handleSelectChange,
            placeholder: '\u8BF7\u9009\u62E9'.concat(label),
          },
          rest,
        ),
        {},
        {
          style: {
            width: 'calc(50% - 5px)',
            marginRight: 10,
          },
        },
      ),
      (options || []).map(function (option) {
        return /*#__PURE__*/ _react.default.createElement(
          _antd.Select.Option,
          {
            key: option.value,
            value: option.value,
          },
          option.label,
        );
      }),
      /*#__PURE__*/ _react.default.createElement(
        _antd.Select.Option,
        {
          key: 'other',
          value: otherRef.current,
        },
        '\u5176\u4ED6',
      ),
    ),
    otherChecked
      ? /*#__PURE__*/ _react.default.createElement(_antd.Input, {
          value: otherChecked ? otherValue : undefined,
          onChange: handleInputChange,
          style: {
            width: 'calc(50% - 5px)',
          },
          maxLength: 100,
        })
      : null,
  );
};
var _default = (0, _createField.default)(SelectWithOther);
exports.default = _default;
